package scheduler;

import java.util.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

public class Job {
	String billingSiteRef;
	String scrapeFilename;
	IdentifierPair identifierPairs[] = new IdentifierPair[5];
	AuthenticationPair credentialPairs[] = new AuthenticationPair[5];
	Date lastRunDate = new Date();
	Date lastStatementDate;
	Double lastStatementTotal;
	ScrapeResult latestResult;
	Scrape latestOutput;
	
	DateFormat dfm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	public void buildJob(int i) throws ParseException {

		String userId;
		Scrape latestOutput = new Scrape();

		switch (i) {
		case 1:
			userId = "Oweson";
			this.billingSiteRef = "Johannesburg Municipality";
			this.scrapeFilename = buildFilename(userId, billingSiteRef);

			// Get the non-encrypted pairs.
			for (int j=0; j<5; j++)
			{
				IdentifierPair nonEncryptedPair = new IdentifierPair();
				nonEncryptedPair.loadPair(j, userId);
				this.identifierPairs[j] = nonEncryptedPair;
			}
			// Get the encrypted pairs.
			for (int j=0; j<5; j++)
			{
				AuthenticationPair encryptedPair = new AuthenticationPair();
				encryptedPair.loadPair(j, userId);
				this.credentialPairs[j] = encryptedPair; 
			}
			this.lastRunDate = dfm.parse("2010-06-03 10:15:00");
			this.lastStatementDate = dfm.parse("2010-05-30 00:00:00");
			this.lastStatementTotal = 123.45;
			this.latestResult = ScrapeResult.ACTIVE;

			latestOutput.output = "This is the latest output from the Johannesburg Municipality scrape.";
			this.latestOutput = latestOutput; 
			break;
		case 2:
			userId = "John";
			this.billingSiteRef = "Telkom";
			this.scrapeFilename = buildFilename(userId, billingSiteRef);

			// Get the non-encrypted pairs.
			for (int j=0; j<5; j++)
			{
				IdentifierPair nonEncryptedPair = new IdentifierPair();
				nonEncryptedPair.loadPair(j, userId);
				this.identifierPairs[j] = nonEncryptedPair;
			}
			// Get the encrypted pairs.
			for (int j=0; j<5; j++)
			{
				AuthenticationPair encryptedPair = new AuthenticationPair();
				encryptedPair.loadPair(j, userId);
				credentialPairs[j] = encryptedPair; 
			}
			this.lastRunDate = (Date) dfm.parse("2010-06-16 19:15:00");
			this.lastStatementDate = (Date) dfm.parse("2010-05-01 00:00:00");
			this.lastStatementTotal = 654.32;
			this.latestResult = ScrapeResult.ACTIVE;

			latestOutput.output = "This is the latest output from the Telkom Municipality scrape.";
			this.latestOutput = latestOutput; 
			break;
		case 3:
			userId = "Oweson";
			this.billingSiteRef = "American Express";
			this.scrapeFilename = buildFilename(userId, billingSiteRef);

			// Get the non-encrypted pairs.
			for (int j=0; j<5; j++)
			{
				IdentifierPair nonEncryptedPair = new IdentifierPair();
				nonEncryptedPair.loadPair(j, userId);
				this.identifierPairs[j] = nonEncryptedPair;
			}
			// Get the encrypted pairs.
			for (int j=0; j<5; j++)
			{
				AuthenticationPair encryptedPair = new AuthenticationPair();
				encryptedPair.loadPair(j, userId);
				credentialPairs[j] = encryptedPair; 
			}
			this.lastRunDate = dfm.parse("2010-04-01 06:33:00");
			this.lastStatementDate = dfm.parse("2010-05-22 00:00:00");
			this.lastStatementTotal = 18587.54;
			this.latestResult = ScrapeResult.ACTIVE;

			latestOutput.output = "This is the latest output from the American Express scrape.";
			this.latestOutput = latestOutput; 
			break;
		}
	}

	public static final String DATE_FORMAT_NOW = "yyyy-MM-dd-HHmmss";
	public static String now() {
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW );
		return sdf.format(cal.getTime());
		
	}

	public static Date today() {
		Calendar cal = Calendar.getInstance();
		return cal.getTime();
	}

	public String buildFilename(String userId, String billingSiteRef) {
		String filename = null;
		String fullFilename = null;
		long now = new Date(System.currentTimeMillis()).getTime();
		
		//Replace any spaces with underscores in billingSiteRef
		filename = now() + "_" + userId + "_" + billingSiteRef + ".xml";
		fullFilename = filename.replaceAll(" ", "_");
		return fullFilename;
	}

	public void print() {
		System.out.println("<---Start of Job-->");
		System.out.println("billingSiteRef: " + this.billingSiteRef);
		System.out.println("scrapeFilename [" + this.scrapeFilename + "]");
		for (int i=0;i<5;i++)
			this.identifierPairs[i].print();	
		for (int i=0;i<5;i++)
			this.credentialPairs[i].print();	
		System.out.println("lastRunDate:       " + dfm.format(this.lastRunDate));
		System.out.println("lastStatementDate: " + dfm.format(this.lastStatementDate));
		System.out.println("latestResult: " + this.latestResult);
//		System.out.println("latestOutput: " + this.latestOutput);
		this.latestOutput.print();
		System.out.println("<---End of Job-->\n");
	}

	public Date getLastRunDate() {
		return lastRunDate;
	}

	public void setLastRunDate(Date lastRunDate) {
		this.lastRunDate = lastRunDate;
	}

	public Double getLastStatementTotal() {
		return lastStatementTotal;
	}

	public void setLastStatementTotal(Double lastStatementTotal) {
		this.lastStatementTotal = lastStatementTotal;
	}

	public void retrieveJob(String userId, String billingSiteRef) {
		try {
			if (userId == "Oweson")
			{
				if (billingSiteRef == "Johannesburg Municipality")
					this.buildJob(1);
				else
					buildJob(3);

			}
			else {
				buildJob(2);
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
